package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.SellCarBean;
import com.project.mapper.ISellCarMapper;
import com.project.service.ISellCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SellCarServiceImpl implements ISellCarService {
    @Autowired
    private ISellCarMapper sellCarMapper;
    @Override
    public List<SellCarBean> findAll(Integer userId) {
        return sellCarMapper.findAll(userId);
    }

    @Override
    public void add(String productId, Integer userId) {
        QueryWrapper<SellCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_productId",productId);
        qw.eq("fk_userId",userId);
        SellCarBean sellCarBean = sellCarMapper.selectOne(qw);
        if (sellCarBean == null){
            sellCarMapper.insert(new SellCarBean(1,productId,userId));
        }else {
            sellCarBean.setNum(sellCarBean.getNum()+1);
            sellCarMapper.updateById(sellCarBean);
        }
    }

    @Override
    public void del(Integer sellCarId) {
        sellCarMapper.deleteById(sellCarId);
    }

    @Override
    public void updateNum(Integer sellCarId, Integer num) {
        SellCarBean sellCarBean = sellCarMapper.selectById(sellCarId);
        sellCarBean.setNum(num);
        sellCarMapper.updateById(sellCarBean);
    }
}
